package com.acme.rogelio;

import com.acme.entity.Tramo;
import com.acme.entity.ZonaRiesgo;
import com.acme.entity.util.DBManager;
import java.awt.Color;
import java.awt.Font;
import java.util.HashMap;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.JTextComponent;
import org.jdesktop.beansbinding.Binding;
import org.jdesktop.xswingx.PromptSupport;

public class PanelZonaRiesgo extends javax.swing.JPanel implements ChangeListener {

    /** Creates new form ZonaRiesgo */
    public PanelZonaRiesgo() {
        initComponents();
        establecerMensajes();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager1 = DBManager.getEntityManager();
        query1 = java.beans.Beans.isDesignTime() ? null : ((javax.persistence.EntityManager)null).createQuery("SELECT t FROM Tramo t");
        listaTramo = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : query1.getResultList();
        queryTabla = java.beans.Beans.isDesignTime() ? null : entityManager1.createQuery("SELECT z FROM ZonaRiesgo z");
        listaZonas = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : queryTabla.getResultList();
        zonaRiesgo1 = new com.acme.entity.ZonaRiesgo();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jButton2 = new javax.swing.JButton();
        cmpDescripcion = new javax.swing.JTextField();
        cmpKilometro = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jToolBar1.setRollover(true);

        jButton1.setText("AGREGAR");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);
        jToolBar1.add(jSeparator1);

        jButton2.setText("ELIMINAR");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.descripcion}"), cmpDescripcion, org.jdesktop.beansbinding.BeanProperty.create("text"), "cmpDescripcion");
        bindingGroup.addBinding(binding);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.kilometro}"), cmpKilometro, org.jdesktop.beansbinding.BeanProperty.create("text"), "cmpKilometro");
        bindingGroup.addBinding(binding);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaTramo, jComboBox2);
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jTable1, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.tramo}"), jComboBox2, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox2ActionPerformed(evt);
            }
        });

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, listaZonas, jTable1, "TablaRiesgo");
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tramo}"));
        columnBinding.setColumnName("Tramo");
        columnBinding.setColumnClass(com.acme.entity.Tramo.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${kilometro}"));
        columnBinding.setColumnName("Kilometro");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${descripcion}"));
        columnBinding.setColumnName("Descripcion");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();

        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cmpDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cmpKilometro, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)))
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 546, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmpKilometro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmpDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
      ZonaRiesgo nuevo= new ZonaRiesgo ();

         nuevo.addChangeListener(this);
         bindingGroup.unbind();
         listaZonas.add(nuevo);
         bindingGroup.bind();
         int fila = listaZonas.size()-1;
         jTable1.setRowSelectionInterval(fila, fila);
         cmpDescripcion.setEnabled(true);
         cmpKilometro.setEnabled(true);
         jComboBox2.setEnabled(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    int fila = jTable1.getSelectedRow();
        if (fila != -1) {
            String mesg = "¿Quieres eliminar lo agregado %s?";
            ZonaRiesgo actual = listaZonas.get(fila);
            int resp = JOptionPane.showConfirmDialog(this, String.format(mesg,actual), "CONFIRMACION", JOptionPane.YES_NO_OPTION);
            if (resp == JOptionPane.YES_OPTION) {
                Binding bin = bindingGroup.getBinding("TablaRiesgo");
                bin.unbind();
                listaZonas.remove (actual);
                cmpDescripcion.setEnabled(false);
                cmpKilometro.setEnabled(false);
                jComboBox2.setEnabled(false);
                bin.bind();
    }//GEN-LAST:event_jButton2ActionPerformed
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cmpDescripcion;
    private javax.swing.JTextField cmpKilometro;
    private javax.persistence.EntityManager entityManager1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private java.util.List<Tramo> listaTramo;
    private java.util.List<ZonaRiesgo> listaZonas;
    private javax.persistence.Query query1;
    private javax.persistence.Query queryTabla;
    private com.acme.entity.ZonaRiesgo zonaRiesgo1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private final static HashMap<String, String> mapa = new HashMap<String, String>();

    static {
        mapa.put("cmpDescripcion", "<Descripcion>");
        mapa.put("cmpKilometro", "<Km>");
        
    }

    private void establecerMensajes() {
        for (Binding bin : bindingGroup.getBindings()) {
            Object obj = bin.getTargetObject();
            JTextComponent cmp = null;
            if (obj instanceof JTextComponent ) {
                cmp = (JTextComponent) obj;
            }
            if (cmp != null) {
                String mensaje = mapa.get(bin.getName());
                PromptSupport.setFontStyle(Font.ITALIC, cmp);
                PromptSupport.setForeground(Color.RED, cmp);
                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, cmp);
                PromptSupport.setPrompt(mensaje, cmp);
            }

        }


    }

    @Override
    public void setEnabled(boolean enabled) {
        for (Binding bin : bindingGroup.getBindings()) {
            JComponent cmp = (JComponent) bin.getTargetObject();
            cmp.setEnabled(enabled);
        }
    }

    @Override
    public void stateChanged(ChangeEvent e) {
         Binding bin = bindingGroup.getBinding("TablaRiesgo");
       bin.unbind();
       bin.bind();
    }
}
